package com.example.userclient.fragment

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.SaleBookListAdapter
import com.example.userclient.activity.BookDetailActivity
import com.example.userclient.databinding.FragmentShopBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

/**
 * https://www.geeksforgeeks.org/arrayadapter-in-android-with-example/
 */


class ShopFragment : Fragment() {
    // TODO: Rename and change types of parameters
        private var binding:FragmentShopBinding?=null
     val GET_DATA_SUCCESS = 1
     val NETWORK_ERROR = 2
     val SERVER_ERROR = 3
     var callBackHandler = Handler(Looper.getMainLooper()){
        when(it.what){
            GET_DATA_SUCCESS -> {
//                val bitmap = it.obj as Bitmap
                SaleBookListAdapter.dataSet = ClientInfoHolder.liveBook?.toTypedArray()
                binding!!.list.adapter!!.notifyDataSetChanged()
                binding!!.swipeRefreshLayout.isRefreshing = false
                Log.i("CustomImageView","setBitMap")
                return@Handler true
            }
            NETWORK_ERROR -> {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                Log.i("CustomImageView","NETWORK_ERROR")
                return@Handler true
            }
            SERVER_ERROR -> {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                Log.i("CustomImageView","SERVER_ERROR")
                return@Handler true
            }
            else -> {
                Toast.makeText(getContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                Log.i("CustomImageView","else")
                return@Handler true
            }
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_shop, container, false)
        val bind =  DataBindingUtil.inflate<FragmentShopBinding>(
            inflater,
                R.layout.fragment_shop,
            container,
            false
        )
        binding = bind
        return bind.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        ClientInfoHolder.loadNetBook(callBackHandler)

        SaleBookListAdapter.setOnItemClickListener(object : SaleBookListAdapter.OnItemClickListener {
            override fun onItemClick(itemView: View?, position: Int) {
                activity?.let {
                    SaleBookListAdapter.dataSet?.get(position)?.let { it1 ->
                        BookDetailActivity.actionStart(it,
                                it1
                        )
                    }
                }
            }
        })

        SaleBookListAdapter.renewDataSet(ClientInfoHolder.liveBook!!.toMutableList())
        SaleBookListAdapter.setHasStableIds(true)
        binding!!.list.adapter = SaleBookListAdapter
        (binding!!.list.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        binding!!.list.layoutManager = StaggeredGridLayoutManager(2,OrientationHelper.VERTICAL)

        binding!!.swipeRefreshLayout.setOnRefreshListener {
            Log.i("ShopFragment", "onRefresh called from SwipeRefreshLayout")
            ClientInfoHolder.loadNetBook(callBackHandler)

        }






    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment ShopFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance() =
            ShopFragment()
//                .apply {
//                arguments = Bundle().apply {
//                    putString(ARG_PARAM1, param1)
//                    putString(ARG_PARAM2, param2)
//                }
//            }
    }
}